#include <interfacelib/window/Gage.h>

using namespace interfacelib::Window;

TimeGage::TimeGage()
{
}

TimeGage::~TimeGage()
{
}

bool TimeGage::create(const char *filename, int x, int y, DWORD attribute )
{
	bool result = Window::create( filename, x, y, attribute );
	m_width = getWidth();
	m_height = getHeight();
	setVisible( false );

	return ( result );
}

void TimeGage::start( int elapTime , bool mode, bool direct )
{
	m_mode = mode;
	m_direct = direct;
	if ( elapTime == -1 )
	{
		setVisible( false );
	}
	else
	{
		setVisible( true );

		m_startTime = timeGetTime();
		m_finishTime = m_startTime + elapTime;

		update();
	}
}

bool TimeGage::update()
{
	if ( !isVisible() ) return ( true );

	DWORD currentTime = timeGetTime();

	if ( m_finishTime < currentTime )
	{
		return ( false );

	}
	else
	{
		DWORD elapTime = currentTime - m_startTime;

		if ( m_direct )
		{
			int width = ( int )( ( ( float )( elapTime ) * ( float )( m_width ) / ( float )( m_finishTime - m_startTime ) )  );

			POINT pt;
			getWindowPos( &pt , false );
			if ( m_mode ) setWindowPos( pt.x, pt.y, m_width - width, -1 );
			else setWindowPos( pt.x, pt.y, width, -1 );
		}
		else
		{
			int height = ( int )( ( ( float )( elapTime ) * ( float )( m_height ) / ( float )( m_finishTime - m_startTime ) )  );

			POINT pt;
			getWindowPos( &pt , false );
			if ( m_mode ) setWindowPos( pt.x, pt.y, -1, m_height - height );
			else setWindowPos( pt.x, pt.y, -1, height );
		}
	}
	return ( true );
}


StepGage::StepGage()
{
}

StepGage::~StepGage()
{
}

bool StepGage::create(const char *filename, int x, int y, DWORD attribute )
{
	bool result = Window::create( filename, x, y, attribute );
	m_width = getWidth();
	m_height = getHeight();

	return ( result );
}

void StepGage::start( int total, int currentPos , bool mode, bool direct )
{
	m_mode = mode;
	m_direct = direct;
	m_currentCount = currentPos;
	if ( total == -1 )
	{
		setVisible( false );
	}
	else
	{
		setVisible( true );

		m_totalCount = total;

		update();
		m_currentCount = currentPos;
	}
}

bool StepGage::update()
{
	if ( !isVisible() ) return ( true );

	if ( m_totalCount < m_currentCount )
	{
		return ( false );

	}
	else
	{
		if ( m_direct )
		{
			int width = ( int )( ( ( float )( m_currentCount ) * ( float )( m_width ) / ( float )( m_totalCount ) )  );

			POINT pt;
			getWindowPos( &pt , false );
			if ( m_mode ) setWindowPos( pt.x, pt.y, m_width - width, -1 );
			else setWindowPos( pt.x, pt.y, width, -1 );
		}
		else
		{
			int height = ( int )( ( ( float )( m_currentCount ) * ( float )( m_height ) / ( float )( m_totalCount ) )  );

			POINT pt;
			getWindowPos( &pt , false );
			if ( m_mode ) setWindowPos( pt.x, pt.y, -1, m_height - height );
			else setWindowPos( pt.x, pt.y, -1, height );
		}
	}
	m_currentCount++;

	return ( true );
}